package com.bluesky.android.webservice;

import java.io.File;

import org.ksoap2.serialization.SoapObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Environment;

import com.bluesky.android.utils.*;


public class MangaStory {
	private int mangaId = 0;
	private String link = "";
    private String title = "";
    private String author = "";
    private String imageLink = "";
    private String summary = "";
    public static MangaStory GetMangaStoryFromSoapObject(SoapObject soapObject){
    	MangaStory mangaStory = new MangaStory();
		if(soapObject.getProperty("Author") != null)
		{
			mangaStory.setAuthor(soapObject.getProperty("Author").toString() );
		}
		if(soapObject.getProperty("ImageLink") != null)
		{
			mangaStory.setImageLink(soapObject.getProperty("ImageLink").toString());
		}
		if(soapObject.getProperty("Link") != null)
		{
			mangaStory.setLink(soapObject.getProperty("Link").toString());
		}
		if(soapObject.getProperty("Summary") != null)
		{
			mangaStory.setSummary(soapObject.getProperty("Summary").toString());
		}
		if(soapObject.getProperty("Title") != null)
		{
			mangaStory.setTitle(soapObject.getProperty("Title").toString());
		}
    	return mangaStory;
    	
    }
    public MangaStory(int mangaId, String link, String title, String author, String imageLink, String summay){
    	this.mangaId = mangaId;
    	this.link = link;
    	this.title = title;
    	this.author = author;
    	this.imageLink = imageLink;
    	this.summary = summay;
    }
    public MangaStory(String link, String title, String author, String imageLink, String summay){    	
    	this.link = link;
    	this.title = title;
    	this.author = author;
    	this.imageLink = imageLink;
    	this.summary = summay;
    }
	public MangaStory() {
		// TODO Auto-generated constructor stub
	}
	public void setLink(String link) {
		if(link != null)
			this.link = link;
	}
	public String getLink() {
		return link;
	}
	public void setSummary(String summary) {
		if(summary != null)
			this.summary = summary;
	}
	public String getSummary() {
		return summary;
	}
	public void setImageLink(String imageLink) {
		if(imageLink != null)
			this.imageLink = imageLink;
	}
	public String getImageLink() {
		return imageLink;
	}
	public void setAuthor(String author) {
		if(author != null)
			this.author = author;
	}
	public String getAuthor() {
		return author;
	}
	public void setTitle(String title) {
		if(title!= null)
			this.title = title;
	}
	public String getTitle() {
		return title;
	}
	public int getMangaId() {
		return mangaId;
	}
	public void setMangaId(int mangaId) {
		this.mangaId = mangaId;
	}
	public String getRootDirectory(){
		
		if(getMangaId() > 0){
			File dir = new File(Environment.getExternalStorageDirectory(), "MangaReader");
			if(!dir.exists()){
				dir.mkdir();
			}
			dir = new File(dir, MangaUtility.md5(getTitle() ) );
			if(!dir.exists()){
				dir.mkdir();
			}
			return dir.getPath();
		}
		return "";
	}
	
	public static MangaStory cursorToMangaStory(Cursor cursor) {
		MangaStory mangaStory = new MangaStory();
		mangaStory.setMangaId(cursor.getInt(cursor.getColumnIndex(MangaContants.MANGA_ID) ) );
		mangaStory.setImageLink(cursor.getString(cursor.getColumnIndex(MangaContants.MANGA_IMAGE_LINK)));
		mangaStory.setLink(cursor.getString(cursor.getColumnIndex(MangaContants.MANGA_LINK) ) );
		mangaStory.setAuthor(cursor.getString(cursor.getColumnIndex(MangaContants.MANGA_AUTHOR) ) );		
		mangaStory.setTitle(cursor.getString(cursor.getColumnIndex(MangaContants.MANGA_TITLE) ) );
		mangaStory.setSummary(cursor.getString(cursor.getColumnIndex(MangaContants.MANGA_SUMMARY) ) );
		return mangaStory;
	}
	public static ContentValues mangaStoryToContentValue(MangaStory mangaStory){
		ContentValues value = new ContentValues();
		value.put(MangaContants.MANGA_TITLE, mangaStory.getTitle() );
		value.put(MangaContants.MANGA_IMAGE_LINK, mangaStory.getImageLink() );
		value.put(MangaContants.MANGA_AUTHOR, mangaStory.getAuthor() );
		value.put(MangaContants.MANGA_LINK, mangaStory.getLink() );
		value.put(MangaContants.MANGA_SUMMARY, mangaStory.getSummary() );
		return value;
	}
}
